package com.azusa.xbiao.controller;

import com.azusa.xbiao.entity.Address;
import com.azusa.xbiao.entity.Result;
import com.azusa.xbiao.entity.StatusCode;
import com.azusa.xbiao.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 查询id用户的收货地址
     * @param uid
     * @return
     */
    @GetMapping("findUserAll/{id}")
    public Result findUserAll(@PathVariable("id")int uid){
        return new Result(StatusCode.SUCCESS,addressService.findUserAll(uid));
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @RequestMapping("deleteAddress/{aid}")
    public Result deleteAddressById(@PathVariable("aid")int id){
        addressService.deleteAddressById(id);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 修改默认地址
     * @param aid
     * @param uid
     * @return
     */
    @PutMapping("updateDefaults/{aid}/{uid}")
    public Result updateDefaults(@PathVariable("aid")int aid,@PathVariable("uid")int uid){
        addressService.updateDefaults(aid,uid);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 新增收货地址
     * @param uid
     * @param params
     * @return
     */
    @PostMapping("insertAddress/{uid}")
    public Result insertAddress(@PathVariable("uid")int uid,@RequestBody Map<String,String> params){
        addressService.insertAddress(uid,params);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     *  根据用户id和地址id查询具体地址
     * @param aid
     * @param uid
     * @return
     */
    @GetMapping("findAddressById/{aid}/{uid}")
    public Result findAddressById(@PathVariable("aid")int aid,@PathVariable("uid")int uid){
        return new Result(StatusCode.SUCCESS,addressService.findAddressById(aid,uid));
    }

    /**
     * 修改收货地址
     * @param address
     * @return
     */
    @PutMapping("updateAddress")
    public Result updateAddress(@RequestBody Address address){
        return new Result(StatusCode.SUCCESS,addressService.updateAddress(address));
    }
}
